package com.eshipenet.shipnet.dp_decoder

import org.json4s.JsonAST.JValue
import org.json4s.native.JsonMethods
import org.json4s.{DefaultFormats, Formats}

import scala.util.Try

object Decoder {

  implicit lazy val formats: Formats = DefaultFormats

  def decode(msg: EdgeChannelDataPacketUncompressedKafkaMessage): Try[EdgeChannelDataPacket] = {
    msg.encoder match {
      case "JSON" => decodeJSON(msg)
      case "PB"   => ???
      case _      => ???
    }
  }

  def decodeJSON(msg: EdgeChannelDataPacketUncompressedKafkaMessage): Try[EdgeChannelDataPacket] = Try {
    val json = JsonMethods.parse(new String(msg.data))
    val data = json.extract[Seq[JValue]]
    EdgeChannelDataPacket(msg.id, data, msg.aux)
  }

}
